//
//  ExperienceViewController.m
//  星期6
//
//  Created by 宗镇荣 on 16/2/25.
//  Copyright © 2016年 zzr. All rights reserved.
//

#import "ExperienceViewController.h"
#import "SaturdayViewController.h"

@interface ExperienceViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic) UITableView *tableView2;
@property (nonatomic) NSMutableArray *data2;
@property (nonatomic,assign)NSInteger currentPage;
@property (nonatomic) MBProgressHUD *hud;


@end

@implementation ExperienceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self refreshData];
}
- (void)refreshData {
    
    _currentPage = 1;
    [self downloadData];
}

-(void)loadMore{
    
    _currentPage++;
    [self downloadData];
}

- (void)downloadData {

    [self.view addSubview:_hud];
    [self.hud show:YES];
    [SelectShopping requestWithPageNumber:_currentPage WithAppId:13 successBlock:^(NSMutableArray *array, NSError *error) {
        
        if (error == nil) {
            
            if (_currentPage == 1) {
                
                [self.data2 removeAllObjects];
            }
            
            [self.data2 addObjectsFromArray:array];
            
            [self.tableView2 reloadData];
            [_tableView2.header endRefreshing];
            [_tableView2.footer endRefreshing];
            
        }else {
            
            QFLog(@"%@", error);
        }
        
        [self.hud hide:YES];
    }];
    
}



- (void)configUI {

    _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 -49)];
    self.tableView2.delegate = self;
    self.tableView2.dataSource = self;
    self.tableView2.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView2];
    
    [_tableView2 registerClass:[SelectTableViewCell class] forCellReuseIdentifier:@"ceLL"];
    
    _tableView2.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    _tableView2.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self loadMore];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.data2.count;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 170;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectShopping *model  = self.data2[indexPath.row];
    DetailTableViewController *detail = [[DetailTableViewController alloc] init];
    detail.url = model.url;
    detail.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detail animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ceLL" forIndexPath:indexPath];
    cell.model = self.data2[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
